﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using Newtonsoft.Json;

namespace Bot.Domain.Categories.Service
{
    public class AnswerInChannelFormFieldDomainService : IAnswerInChannelFormFieldDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, AnswerInChannelFormField> _repository;

        public AnswerInChannelFormFieldDomainService(IRepository<Guid, AnswerInChannelFormField> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public AnswerInChannelFormField Create(AnswerInChannelFormField category)
        {
            return _repository.Create(category);
        }

        public AnswerInChannelFormField Get(Guid id ,string include = "")
        {
            var field = _repository.Get(id);
            if(field!=null
                && !string.IsNullOrEmpty(include)
                && include.Split(',').Contains("entity")
                && field.EntityId.HasValue)
            {
                IEntityDomainService entityDomainService = _container.Resolve<IEntityDomainService>();
                field.Entity = entityDomainService.Get(field.EntityId.Value, include);
            }
            return field;
        }

        public AnswerInChannelFormField Update(AnswerInChannelFormFieldUpdateBo bo)
        {
            AnswerInChannelFormField answerInChannelFormField = _repository.Get(bo.Id);
            answerInChannelFormField.Id = bo.Id;
            answerInChannelFormField.EntityId = bo.EntityId;
            answerInChannelFormField.Name = bo.Name;
            answerInChannelFormField.EntityLabel = bo.EntityLabel;
            answerInChannelFormField.Type = bo.Type;
            answerInChannelFormField.IsRequired = bo.IsRequired;
            answerInChannelFormField.IsMasked = bo.IsMasked;
            answerInChannelFormField.Options = JsonConvert.SerializeObject(bo.Options);
            _repository.Update(answerInChannelFormField);
            return answerInChannelFormField;

        }

        public IEnumerable<AnswerInChannelFormField> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            if (_repository.Exists(id))
            {
                AnswerInChannelFormField answerInChannelFormField = _repository.Get(id);

                _repository.Delete(answerInChannelFormField);
            }

        }

        public IEnumerable<AnswerInChannelFormField> GetByFormId(Guid formId, string include = "")
        {
            var list = _repository.List(new AnswerInChannelFormFieldFilterSpecification(formId)).ToList();
            if (list != null && list.Count > 0 && !string.IsNullOrEmpty(include) && include.Split(',').Contains("entity"))
            {
                IEntityDomainService entityDomainService = _container.Resolve<IEntityDomainService>();
                foreach (var item in list)
                {
                    if (item.EntityId.HasValue && item.EntityId != Guid.Empty)
                    {
                        item.Entity = entityDomainService.Get(item.EntityId.Value,include);
                    }
                }
            }
            return list;
        }
    }
}
